package com.lxhjh.springboot.demo.core;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.lxhjh.springboot.demo.job.HelloJob;



/**
 * 启动Quartz
 * @author lianghui
 *
 */
@Component
public class QuartzLaunchService {
	private static Logger logger = LoggerFactory.getLogger(QuartzLaunchService.class);
	
	
	@Resource(name = "scheduler")
	private Scheduler scheduler;
	
	@PostConstruct
	public void init() {
		logger.info("加载定时任务开始");
		try {
			//Scheduler scheduler = schedulerBean.getScheduler();
			TriggerKey triggerKey = TriggerKey.triggerKey("quartz-lxh", Scheduler.DEFAULT_GROUP);
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/10 * * * * ?");
			CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
			JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withDescription("Hello定时器").withIdentity("hello-job", Scheduler.DEFAULT_GROUP).build();
			jobDetail.getJobDataMap().put("name", "quartz-lxh");
//			jobDetail.getJobDataMap().put("executorNo", task.getExecutorNo());
//			jobDetail.getJobDataMap().put("sendType", task.getSendType());
//			jobDetail.getJobDataMap().put("url", task.getUrl());
//			jobDetail.getJobDataMap().put("executeParamter", task.getExecuteParamter());
	        
			scheduler.scheduleJob(jobDetail, trigger);
			logger.info("task 'quartz-lxh' schedulerRule :'*/10 * * * * ?' reload succeed");
		} catch (Exception e) {
			logger.info("定时任务加载失败，请手动加载！\n");
			logger.info("定时任务加载异常:", e);
		}
		logger.info("定时任务加载完成");
	}
}
